﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Relay.UI.Controls.Docking.Primitives {
    [TemplatePart(Name = "PART_TopButton", Type = typeof(FrameworkElement)),
    TemplatePart(Name = "PART_LeftButton", Type = typeof(FrameworkElement)),
    TemplatePart(Name = "PART_RightButton", Type = typeof(FrameworkElement)),
    TemplatePart(Name = "PART_BottomButton", Type = typeof(FrameworkElement)),
    TemplatePart(Name = "PART_CenterButton", Type = typeof(FrameworkElement))]
    internal class DockElementOverlay : DockOverlay, IDockOverlay {
        #region Fields
        private FrameworkElement _topButton = null;
        private FrameworkElement _leftButton = null;
        private FrameworkElement _rightButton = null;
        private FrameworkElement _bottomButton = null;
        private FrameworkElement _centerButton = null;
        #endregion

        #region Constructor
        static DockElementOverlay() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DockElementOverlay),
                new FrameworkPropertyMetadata(typeof(DockElementOverlay)));
        }
        #endregion

        #region Overrides
        public override void OnApplyTemplate() {
            base.OnApplyTemplate();

            _topButton = Template.FindName("PART_TopButton", this) as FrameworkElement;
            _leftButton = Template.FindName("PART_LeftButton", this) as FrameworkElement;
            _rightButton = Template.FindName("PART_RightButton", this) as FrameworkElement;
            _bottomButton = Template.FindName("PART_BottomButton", this) as FrameworkElement;
            _centerButton = Template.FindName("PART_CenterButton", this) as FrameworkElement;
        }
        #endregion

        #region IDockHint Members

        public DockOverlayHitTestResult DockHitTest(Point mousePos) {
            if (_topButton != null && HitTestElement(_topButton, mousePos))
                HitTest = DockOverlayHitTestResult.Top;
            else if (_leftButton != null && HitTestElement(_leftButton, mousePos))
                HitTest = DockOverlayHitTestResult.Left;
            else if (_rightButton != null && HitTestElement(_rightButton, mousePos))
                HitTest = DockOverlayHitTestResult.Right;
            else if (_bottomButton != null && HitTestElement(_bottomButton, mousePos))
                HitTest = DockOverlayHitTestResult.Bottom;
            else if (_centerButton != null && HitTestElement(_centerButton, mousePos))
                HitTest = DockOverlayHitTestResult.Center;
            else
                HitTest = DockOverlayHitTestResult.None;

            return HitTest;
        }

        #endregion
    }
}
